﻿using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using FBProject.BusinessLogic;
using FBProject.BusinessLogic.FBImport;

namespace FBProject
{
    public partial class FBExportFileSelector : UserControl
    {

        public event EventHandler FBExportFileChanged;

        public bool FBExportFileOk 
        {
            get
            {
                return File.Exists(FBExportFileNameTextBox.Text);
            }
        }

        public string FBExportFileName
        {
            get { return FBExportFileNameTextBox.Text; }
            set 
            { 
                FBExportFileNameTextBox.Text = value;
                FBExportFileNameTextBox.Select(FBExportFileNameTextBox.Text.Length, 0);
            }
        }
        

        public FBExportFileSelector()
        {
            InitializeComponent();
        }

        private void FBExportFileSelector_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                FBExportFileName = FBCsvImport.GetLatestFBCsvFile(ApplicationConfiguration.FBFilePath, ApplicationConfiguration.FBFilePattern);
                RefreshFBExportFileInfo();
            }
        }

        private void SelectFBExportFileButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(FBExportFileNameTextBox.Text))
            {
                System.IO.FileInfo fi = new FileInfo(FBExportFileNameTextBox.Text);
                if (Directory.Exists(fi.DirectoryName))
                {
                    FBExportFileOpenFileDialog.InitialDirectory = fi.DirectoryName;
                }
            }
            else
            {
                FBExportFileOpenFileDialog.InitialDirectory = ApplicationConfiguration.FBFilePath;
            }
            if (FBExportFileOpenFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                FBExportFileName = FBExportFileOpenFileDialog.FileName;
                var fi = new FileInfo(FBExportFileNameTextBox.Text);
                if (Directory.Exists(fi.DirectoryName))
                {
                    ApplicationConfiguration.FBFilePath = fi.DirectoryName;
                }
            }
        }

        private void FBExportFileNameTextBox_TextChanged(object sender, EventArgs e)
        {
            FBExportFileNameTextBox.ForeColor = this.FBExportFileOk ? SystemColors.WindowText : Color.Red;
            OnFBExportFileChanged();
            RefreshFBExportFileInfo();
        }

        private void RefreshFBExportFileInfo()
        {
            if (FBExportFileOk)
            {
                var fileInfo = new FileInfo(FBExportFileNameTextBox.Text);
                CreatedLabel.Text = ApplicationHelper.GetFormattedDateTimeDifference(fileInfo.CreationTime);
                SizeLabel.Text = ApplicationHelper.GetFormattedFileSize(fileInfo.Length);
            }
            else
            {
                CreatedLabel.Text = string.Empty;
                SizeLabel.Text = string.Empty;
            }
        }

        private void OnFBExportFileChanged()
        {
            if (FBExportFileChanged != null)
            {
                FBExportFileChanged(this, EventArgs.Empty);
            }
        }

        private void FBExportFileSelector_DragDrop(object sender, DragEventArgs e)
        {
            var data = e.Data.GetData(DataFormats.FileDrop) as string[];
            if ((data != null) && (data.Length > 0))
            {
                FBExportFileName = data[0];
            }
        }

        private void FBExportFileSelector_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

    }
}
